package com.tbit.main.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.*;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.ChargeHouse;
import com.tbit.main.pojo.Point;
import com.tbit.main.service.CalibrateService;
import com.tbit.main.service.ChargeHouseService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 充电坊
 *
 * @author LMY
 * @create 2020-11-12 9:18
 */
@RestController
@RequestMapping("/chargeHouse")
public class ChargeHouseController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private ChargeHouseService chargeHouseService;

    /**
     * 添加
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.CHARGEHOUSE, methods = MethodEnum.CHARGEHOUSE_ADD)
    public JsonResult add(String token, ChargeHouse chargeHouse, Integer mapType) {

        Assert.notNull(chargeHouse.getAccountId(), "AccountUser.agentId.notNull");

        /**检验*/
        Assert.notNull(chargeHouse.getChargeHouseName(), "ParkPoint.name.notNull");
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        /**保存经纬度*/
        Assert.notNull(chargeHouse.getLo(), "Map.lon.nutNull");
        Assert.notNull(chargeHouse.getLa(), "Map.lat.nutNull");
        Point point = new Point(chargeHouse.getLo(), chargeHouse.getLa());

        chargeHouse.setLo(point.getX());
        chargeHouse.setLa(point.getY());
        /** 保存高德地图原始经纬度 */
        chargeHouse.setLoC(point.getX());
        chargeHouse.setLaC(point.getY());

        /** 经纬度校准 */
        calibrateService.calChargeHouse(chargeHouse, mapType, SysConstant.ORIGINAL);

        /** 备注 */
        if (chargeHouse.getRemark() == null) {
            chargeHouse.setRemark("");
        }

        chargeHouseService.insert(chargeHouse);
        return JsonResult.succ();
    }

    /**
     * 更改
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.CHARGEHOUSE, methods = MethodEnum.CHARGEHOUSE_UPDATE)
    public JsonResult update(String token, ChargeHouse chargeHouse, Integer mapType) {

        /**检验*/
        Assert.notNull(chargeHouse.getChargeHouseId(), "ChargeHouse.control.chargeHouseId.notNull");
        Assert.notNull(chargeHouse.getChargeHouseName(), "ChargeHouse.control.chargeHouseName.notNull");
        Assert.notNull(chargeHouse.getAccountId(), "AccountUser.agentId.notNull");

        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        /**保存经纬度*/
        Assert.notNull(chargeHouse.getLo(), "Map.lon.nutNull");
        Assert.notNull(chargeHouse.getLa(), "Map.lat.nutNull");
        Point point = new Point(chargeHouse.getLo(), chargeHouse.getLa());

        chargeHouse.setLo(point.getX());
        chargeHouse.setLa(point.getY());
        /** 保存高德地图原始经纬度 */
        chargeHouse.setLoC(point.getX());
        chargeHouse.setLaC(point.getY());

        /** 经纬度校准 */
        calibrateService.calChargeHouse(chargeHouse, mapType, SysConstant.ORIGINAL);

        /** 备注 */
        if (chargeHouse.getRemark() == null) {
            chargeHouse.setRemark("");
        }
        /**查询充电坊*/
        ChargeHouse chargeHouseDB = chargeHouseService.getByChargeHouseId(chargeHouse.getAccountId());

        /**替换可修改字段*/
        ObjectUtil.assign(chargeHouse, chargeHouseDB, null);

        chargeHouseService.update(chargeHouse);

        return JsonResult.succ();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @OperateLog(module = ModuleEnum.CHARGEHOUSE, methods = MethodEnum.CHARGEHOUSE_DEL)
    public JsonResult del(String token, Integer chargeHouseId) {
        /* 校验 */
        Assert.notNull(chargeHouseId, "ChargeHouse.control.chargeHouseId.notNull");

        chargeHouseService.del(chargeHouseId);
        return JsonResult.succ();
    }

    /**
     * 根据平台账号id查询
     */
    @RequestMapping("/getByAccountId")
    public JsonResult byAccoutId(String token, Integer mapType, Integer accountId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        AccountUser accountUserToken = tokenService.getAccount(token);

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        List<ChargeHouse> chargeHouse = chargeHouseService.getByAccountId(accountId);
        if (chargeHouse != null) {
            /** 校准 */
            calibrateService.calibrateChargeHouse(chargeHouse, mapType);
        }

        return JsonResult.succ(chargeHouse);
    }
}
